package com.jingzhi.huimiao.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jingzhi.huimiao.R;
import com.jingzhi.huimiao.WordDetailsActivity;
import com.jingzhi.huimiao.bean.WrongWordInfo;
import com.jingzhi.huimiao.utils.DataStoreUtil;
import com.jingzhi.huimiao.utils.SQLdm;

/**
 * 复习界面
 * 
 * @author cxk
 * 
 */
public class ReviewFragment extends Fragment implements OnClickListener {

	private TextView title;
	private ListView lv_wrongwd;
	private SQLiteDatabase db;
	private Cursor wordcountcur;
	private long userid;
//	private int wordcount;
	private Cursor timecountcur;
	private int timecount;
	private List<String> times;
	private static final int ListView_Head_TYPE = 0;// 时间信息
	private List<WrongWordInfo> wrongwords;
	private ImageView iv_asc;
	private ImageView iv_desc;
	private LinearLayout iv_back;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = View.inflate(getActivity(), R.layout.fragment_tab2, null);
		initview(view);
		title.setText("复习");
		iv_back.setVisibility(View.GONE);
		// 打开数据库输出流
		SQLdm s = new SQLdm();
		db = s.openDatabase(getActivity().getApplicationContext());

		userid = DataStoreUtil.getLong(getActivity(), "userid");

		

		// 查询时间总数
		timecountcur = db.rawQuery(
				"select DISTINCT insert_time from wrong_word where user_id="
						+ userid + " order by insert_time desc;", null);
		iv_desc.setSelected(true);
		showView();
		addListener();
		
		// LvHeightUtil.setListViewHeightBasedOnChildren(lv_wrongwd);

		return view;
	}


	private void addListener() {
		// TODO Auto-generated method stub
		iv_asc.setOnClickListener(this);
		iv_desc.setOnClickListener(this);
	}

	private void showView() {
		
//		Log.e("time----showView---", "showView" + "");
		
		wrongwords = new ArrayList<WrongWordInfo>();
		timecount = timecountcur.getCount();
		times = new ArrayList<String>();
		while (timecountcur.moveToNext()) {
			String time = timecountcur.getString(timecountcur
					.getColumnIndex("insert_time"));
			times.add(time);
			Log.e("time----wrong---", time + "");

			// 根据插入时间查询单词总数
			wordcountcur = db.rawQuery(
					"select * from wrong_word where insert_time='" + time
							+ "';", null);
//			wordcount += wordcountcur.getCount();
			// Log.e("time----wordcount---", wordcount + "");

			while (wordcountcur.moveToNext()) {
				WrongWordInfo wwi = new WrongWordInfo();
				long wordid = wordcountcur.getLong(wordcountcur
						.getColumnIndex("word_id"));

				// 查询单词信息
				Cursor wrongwcur = db.rawQuery("select * from word where id='"
						+ wordid + "';", null);
				if (wrongwcur.moveToFirst()) {
					wwi.word_name = wrongwcur.getString(wrongwcur
							.getColumnIndex("word_name"));
					wwi.word_soundmark = wrongwcur.getString(wrongwcur
							.getColumnIndex("word_soundmark"));
					wwi.word_translation = wrongwcur.getString(wrongwcur
							.getColumnIndex("word_translation"));
					wwi.insert_time = time;
					wrongwords.add(wwi);
				}
				// Log.e("time----wrongwords---", wrongwords + "");
				if (wrongwcur != null) {
					wrongwcur.close();
				}
			}
			if (wordcountcur != null) {
				wordcountcur.close();
			}
		}

		if (timecountcur != null) {
			timecountcur.close();
		}
		
		lv_wrongwd.setAdapter(new ReviewAdapter());
//		lv_wrongwd.setOnItemClickListener(new OnItemClickListener() {

//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				// TODO Auto-generated method stub
//				View itemview = lv_wrongwd.getChildAt(position);
//				LinearLayout item = (LinearLayout) itemview.findViewById(R.id.ll_wrongwd_item);
//				TextView item_wordname = (TextView) itemview.findViewById(R.id.tv_wordname);
//				TextView item_wordsoundmark = (TextView) itemview.findViewById(R.id.tv_wordsoundmark);
//				item.setBackgroundColor(Color.rgb(162, 216, 221));
//				String wordname = (String) item_wordname.getText();
////				String wordname = (String) item_wordname.getText();
////				item_wordname.setT
//			}
//		});
//		lv_wrongwd.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> parent, View view,
//					int position, long id) {
//				// TODO Auto-generated method stub
//				lv_wrongwd.getChildAt(position).setSelected(true);
//				View itemview = lv_wrongwd.getChildAt(position);
//				
//				LinearLayout item = (LinearLayout) itemview.findViewById(R.id.ll_wrongwd_item);
//				TextView item_wordname = (TextView) itemview.findViewById(R.id.tv_wordname);
//				TextView item_wordsoundmark = (TextView) itemview.findViewById(R.id.tv_wordsoundmark);
//				item.setBackgroundColor(Color.rgb(162, 216, 221));
//				String wordname = (String) item_wordname.getText();
//				item_wordname.setText(wrongwords.get(position).word_translation);
//				item_wordsoundmark.setVisibility(View.GONE);
//			}

//			@Override
//			public void onNothingSelected(AdapterView<?> parent) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
	}

	private void initview(View view) {
		title = (TextView) view.findViewById(R.id.tv_title);
		lv_wrongwd = (ListView) view.findViewById(R.id.lv_wrongwd);
		iv_asc = (ImageView) view.findViewById(R.id.iv_asc);
		iv_desc = (ImageView) view.findViewById(R.id.iv_desc);
		iv_back = (LinearLayout) view.findViewById(R.id.iv_back_tomain);
		
	}

	class ReviewAdapter extends BaseAdapter  {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return wrongwords.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder ;
			if (convertView == null ) {
				holder = new ViewHolder();
				convertView = View.inflate(getActivity(),
						R.layout.wrongword_item, null);
				holder.tv_time = (TextView) convertView
						.findViewById(R.id.tv_time);
				holder.tv_wordname = (TextView) convertView
						.findViewById(R.id.tv_wordname);
				holder.tv_wordsoundmark = (TextView) convertView
						.findViewById(R.id.tv_wordsoundmark);
				holder.iv_flag = (ImageView) convertView
						.findViewById(R.id.iv_flag);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			final String xiangqing = wrongwords.get(position).word_name;
			holder.iv_flag.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					DataStoreUtil.put(getActivity(), "xiangqing", xiangqing);
//					Log.e("xiangqing", xiangqing);
					startActivity(new Intent(getActivity(), WordDetailsActivity.class));
					
				}
			});

			// 设置标头时间
			if (position > 0
					&& (wrongwords.get(position).insert_time).equals(wrongwords
							.get(position - 1).insert_time)) {
				holder.tv_time.setVisibility(View.GONE);
			} else {
				holder.tv_time.setVisibility(View.VISIBLE);
				holder.tv_time.setText(wrongwords.get(position).insert_time);
			}
			holder.tv_wordname.setText(wrongwords.get(position).word_name);
			holder.tv_wordsoundmark
					.setText(wrongwords.get(position).word_soundmark);

			return convertView;
		}
		

		class ViewHolder {
			private TextView tv_time;
			private TextView tv_wordname;
			private TextView tv_wordsoundmark;
			private ImageView iv_flag;
		}


	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub

		if (db != null) {
			db.close();
		}
		super.onDestroyView();

	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_asc:
			if(!iv_asc.isSelected()){
				iv_asc.setSelected(true);
				iv_desc.setSelected(false);
				// 查询时间总数
				timecountcur = db.rawQuery(
						"select DISTINCT insert_time from wrong_word where user_id="
								+ userid + " order by insert_time asc;", null);
				showView();
			}else{
				return;
			}
			
			break;
		case R.id.iv_desc:
			if(!iv_desc.isSelected()){
				iv_desc.setSelected(true);
				iv_asc.setSelected(false);
				// 查询时间总数
				timecountcur = db.rawQuery(
						"select DISTINCT insert_time from wrong_word where user_id="
								+ userid + " order by insert_time desc;", null);
				showView();
			}else{
				return;
			}
			
			break;

		default:
			break;
		}
	}
}
